<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="影片名称" prop="movieName">
        <el-input
          v-model="queryParams.movieName"
          placeholder="请输入影片名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上映时间" prop="movieReleaseTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.movieReleaseTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择上映时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="首次上映地区" prop="movieArea">
        <el-input
          v-model="queryParams.movieArea"
          placeholder="请输入首次上映地区"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电影版本" prop="movieVersion">
        <el-input
          v-model="queryParams.movieVersion"
          placeholder="请输入电影版本"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影片海报" prop="movieImg">
        <el-input
          v-model="queryParams.movieImg"
          placeholder="请输入影片海报"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影片英文名" prop="movieEname">
        <el-input
          v-model="queryParams.movieEname"
          placeholder="请输入影片英文名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="评分" prop="movieScore">
        <el-input
          v-model="queryParams.movieScore"
          placeholder="请输入评分"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影片状态0-未上线 1-上线 2-下线" prop="movieStatus">
        <el-select v-model="queryParams.movieStatus" placeholder="请选择影片状态0-未上线 1-上线 2-下线" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="观看次数" prop="movieCount">
        <el-input
          v-model="queryParams.movieCount"
          placeholder="请输入观看次数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="票房金额" prop="movieBoxOffice">
        <el-input
          v-model="queryParams.movieBoxOffice"
          placeholder="请输入票房金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影片时长" prop="movieTime">
        <el-input
          v-model="queryParams.movieTime"
          placeholder="请输入影片时长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="想要观看人数" prop="movieWantCount">
        <el-input
          v-model="queryParams.movieWantCount"
          placeholder="请输入想要观看人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="影片上映年份" prop="movieReleaseYear">
        <el-input
          v-model="queryParams.movieReleaseYear"
          placeholder="请输入影片上映年份"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="累计评论人数" prop="commentUserCount">
        <el-input
          v-model="queryParams.commentUserCount"
          placeholder="请输入累计评论人数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cinema:movie:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cinema:movie:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cinema:movie:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cinema:movie:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="movieList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="movieId" />
      <el-table-column label="影片名称" align="center" prop="movieName" />
      <el-table-column label="上映时间" align="center" prop="movieReleaseTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.movieReleaseTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="首次上映地区" align="center" prop="movieArea" />
      <el-table-column label="影片描述" align="center" prop="movieInfo" />
      <el-table-column label="电影版本" align="center" prop="movieVersion" />
      <el-table-column label="影片海报" align="center" prop="movieImg" />
      <el-table-column label="影片英文名" align="center" prop="movieEname" />
      <el-table-column label="评分" align="center" prop="movieScore" />
      <el-table-column label="影片状态0-未上线 1-上线 2-下线" align="center" prop="movieStatus" />
      <el-table-column label="观看次数" align="center" prop="movieCount" />
      <el-table-column label="票房金额" align="center" prop="movieBoxOffice" />
      <el-table-column label="影片时长" align="center" prop="movieTime" />
      <el-table-column label="想要观看人数" align="center" prop="movieWantCount" />
      <el-table-column label="影片上映年份" align="center" prop="movieReleaseYear" />
      <el-table-column label="累计评论人数" align="center" prop="commentUserCount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cinema:movie:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cinema:movie:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改影片信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="影片名称" prop="movieName">
          <el-input v-model="form.movieName" placeholder="请输入影片名称" />
        </el-form-item>
        <el-form-item label="上映时间" prop="movieReleaseTime">
          <el-date-picker clearable size="small"
            v-model="form.movieReleaseTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择上映时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="首次上映地区" prop="movieArea">
          <el-input v-model="form.movieArea" placeholder="请输入首次上映地区" />
        </el-form-item>
        <el-form-item label="影片描述" prop="movieInfo">
          <el-input v-model="form.movieInfo" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="电影版本" prop="movieVersion">
          <el-input v-model="form.movieVersion" placeholder="请输入电影版本" />
        </el-form-item>
        <el-form-item label="影片海报" prop="movieImg">
          <el-input v-model="form.movieImg" placeholder="请输入影片海报" />
        </el-form-item>
        <el-form-item label="影片英文名" prop="movieEname">
          <el-input v-model="form.movieEname" placeholder="请输入影片英文名" />
        </el-form-item>
        <el-form-item label="评分" prop="movieScore">
          <el-input v-model="form.movieScore" placeholder="请输入评分" />
        </el-form-item>
        <el-form-item label="影片状态0-未上线 1-上线 2-下线">
          <el-radio-group v-model="form.movieStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="观看次数" prop="movieCount">
          <el-input v-model="form.movieCount" placeholder="请输入观看次数" />
        </el-form-item>
        <el-form-item label="票房金额" prop="movieBoxOffice">
          <el-input v-model="form.movieBoxOffice" placeholder="请输入票房金额" />
        </el-form-item>
        <el-form-item label="影片时长" prop="movieTime">
          <el-input v-model="form.movieTime" placeholder="请输入影片时长" />
        </el-form-item>
        <el-form-item label="想要观看人数" prop="movieWantCount">
          <el-input v-model="form.movieWantCount" placeholder="请输入想要观看人数" />
        </el-form-item>
        <el-form-item label="影片上映年份" prop="movieReleaseYear">
          <el-input v-model="form.movieReleaseYear" placeholder="请输入影片上映年份" />
        </el-form-item>
        <el-form-item label="累计评论人数" prop="commentUserCount">
          <el-input v-model="form.commentUserCount" placeholder="请输入累计评论人数" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMovie, getMovie, delMovie, addMovie, updateMovie, exportMovie } from "@/api/cinema/movie";

export default {
  name: "Movie",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 影片信息表格数据
      movieList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        movieName: null,
        movieReleaseTime: null,
        movieArea: null,
        movieInfo: null,
        movieVersion: null,
        movieImg: null,
        movieEname: null,
        movieScore: null,
        movieStatus: null,
        movieCount: null,
        movieBoxOffice: null,
        movieTime: null,
        movieWantCount: null,
        movieReleaseYear: null,
        commentUserCount: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        movieScore: [
          { required: true, message: "评分不能为空", trigger: "blur" }
        ],
        movieCount: [
          { required: true, message: "观看次数不能为空", trigger: "blur" }
        ],
        movieBoxOffice: [
          { required: true, message: "票房金额不能为空", trigger: "blur" }
        ],
        movieWantCount: [
          { required: true, message: "想要观看人数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询影片信息列表 */
    getList() {
      this.loading = true;
      listMovie(this.queryParams).then(response => {
        this.movieList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        movieId: null,
        movieName: null,
        movieReleaseTime: null,
        movieArea: null,
        movieInfo: null,
        movieVersion: null,
        movieImg: null,
        movieEname: null,
        movieScore: null,
        movieStatus: 0,
        movieCount: null,
        movieBoxOffice: null,
        movieTime: null,
        movieWantCount: null,
        movieReleaseYear: null,
        commentUserCount: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.movieId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加影片信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const movieId = row.movieId || this.ids
      getMovie(movieId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改影片信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.movieId != null) {
            updateMovie(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMovie(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const movieIds = row.movieId || this.ids;
      this.$confirm('是否确认删除影片信息编号为"' + movieIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMovie(movieIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有影片信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMovie(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
